<!--富文本封装-->
<template>
  <div style="border: 1px solid #ccc;z-index: 999">
    <Toolbar
        style="border-bottom: 1px solid #ccc"
        :editor="editorRef"
        :defaultConfig="toolbarConfig"
        class="Toolbar"
        mode="simple"

    />
    <Editor
        style="min-height: 100px; overflow-y: hidden;"
        v-model="valueHtml"
        :defaultConfig="editorConfig"
        @onCreated="handleCreated"
        class="Editor"
    />
  </div>
</template>
<script setup>
import '@wangeditor/editor/dist/css/style.css' // 引入 css
import { onBeforeUnmount, ref, shallowRef ,watch,watchEffect} from 'vue'
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import {Common} from "@/network/helpCenterManagement";

//声明属性
const props=defineProps({
  modelValue:{
    type:String,
    default:''
  }
})
//声明事件消息
const emits=defineEmits(['update:modelValue'])


// 编辑器实例，必须用 shallowRef
const editorRef = shallowRef()

// 内容 HTML
    const valueHtml = ref('<p>请输入内容</p>')
//当属性变化时，自动赋值给valueHtml
watchEffect(()=>{
  valueHtml.value=props.modelValue
})
//当valueHtml 变化时，自动告诉父组件
watch(valueHtml,(newHtml)=>{
  emits('update:modelValue',newHtml)
})

// 模拟 ajax 异步获取内容
// onMounted(() => {
//   setTimeout(() => {
//     valueHtml.value = '<p>模拟 Ajax 异步设置内容</p>'
//   }, 1500)
// })


const toolbarConfig = {
  excludeKeys: ["code","insertImage","codeBlock","insertTable","insertVideo","blockquote","todo"]
}
const upload=ref()
const editorConfig = {
  placeholder: '请输入内容...' ,
  MENU_CONF:{
    uploadImage:{
      fieldName:'file',
      async customUpload(file, insertFn) {
        upload.value=file
        const fileData = new FormData();
        fileData.append('file', upload.value)
       await Common(fileData).then((res)=>{
         insertFn(res.data.data.full_url)
       })
      },
    }
  }
}

// 组件销毁时，也及时销毁编辑器
onBeforeUnmount(() => {
  const editor = editorRef.value
  if (editor == null) return
  editor.destroy()
})

const handleCreated = (editor) => {
  editorRef.value = editor // 记录 editor 实例，重要！
}


</script>
<style scoped lang="scss">
//.Toolbar{
//  height: 100px;
//}
</style>